package com.example.aigc_education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.aigc_education.domain.Notice;
import com.example.aigc_education.service.NoticeService;
import com.example.aigc_education.utils.ResultUtil;
import com.example.aigc_education.utils.SecurityUtils;
import com.example.aigc_education.utils.SseServer;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    /**
     * 加载通知
     * @return 返回通知
     */
    @GetMapping("/load")
    public SseEmitter loadNotice() {
        Long userId = SecurityUtils.getUserId();
        SseEmitter connect = SseServer.createConnect(userId);
        long count = noticeService.count(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getToUserId, userId)
                .eq(Notice::getIsRead, 1)
                .eq(Notice::getStatus, 1));
        SseServer.sendMessage(userId, "您有" + count + "条未读通知");
        return connect;
    }


    /**
     * 获取通知
     * @return 返回通知列表
     */
    @GetMapping
    public ResultUtil getNotice() {
        Long userId = SecurityUtils.getUserId();
        List<Notice> noticeList = noticeService.list(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getToUserId, userId)
                .eq(Notice::getStatus, 1));
        return ResultUtil.success(noticeList);
    }

    /**
     * 标记通知为已读
     * @param noticeId 通知ID
     * @return 返回成功
     */
    @PutMapping("/{noticeId}/read")
    public ResultUtil readNotice(@PathVariable Long noticeId) {
        Notice notice = noticeService.getById(noticeId);
        notice.setIsRead(1);
        noticeService.updateById(notice);
        return ResultUtil.success();
    }


    /**
     * 删除通知
     * @param noticeId 通知ID
     * @return 返回成功
     */
    @PutMapping("/{noticeId}/delete")
    public ResultUtil deleteNotice(@PathVariable Long noticeId) {
        Notice notice = noticeService.getById(noticeId);
        notice.setStatus(0);
        noticeService.updateById(notice);
        return ResultUtil.success();
    }

    /**
     * 发送通知
     * @param notice 通知
     * @return 返回成功
     */
    @PostMapping
    public ResultUtil sendNotice(@RequestBody Notice notice) {
        noticeService.save(notice);
        SseServer.sendMessage(notice.getToUserId(), "您有新的通知");
        return ResultUtil.success();
    }

    /**
     * 向特定用户群发送通知
     * @param userIds 用户ID列表
     * @param notice 通知
     * @return 返回成功
     */
    @PostMapping("/sendToUsers")
    public ResultUtil sendNoticeToUsers(@RequestBody List<Long> userIds,
                                        @RequestBody Notice notice) {
        userIds.forEach(userId -> {
            notice.setNoticeId(null);
            notice.setToUserId(userId);
            noticeService.save(notice);
//            SseServer.sendMessage(userId, "您有新的通知");
        });
        SseServer.batchSendMessage(userIds, "您有新的通知");
        return ResultUtil.success();
    }

    /**
     * 获取未读通知数量(可删除)
     * @return 返回未读通知数量
     */
    @GetMapping("/unread")
    public ResultUtil getUnreadNoticeCount() {
        Long userId = SecurityUtils.getUserId();
        long count = noticeService.count(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getToUserId, userId)
                .eq(Notice::getIsRead, 1)
                .eq(Notice::getStatus, 1));
        return ResultUtil.success(count);
    }
}
